package com.czh.gzskzn.shenke3.Activity.ShuiFeiActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.czh.gzskzn.shenke3.Activity.UtilServer.WanZhi;
import com.czh.gzskzn.shenke3.R;
import com.czh.gzskzn.shenke3.ZhiFuBao.utils.Merchant;
import com.czh.gzskzn.shenke3.ZhiFuBao.utils.OrderUtils;
import com.fuqianla.paysdk.FuQianLa;
import com.fuqianla.paysdk.FuQianLaPay;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by gzsk on 2017/5/2.
 * name  陈忠红
 * email czhhx15599109256@163.com
 */
public class ShuiFeiXQ extends AppCompatActivity implements View.OnClickListener{


        private LinearLayout back,xinaQi;
        private TextView yuE,yinJiao,hao,UserName,waiYe,qianfeijine,tvXiqnQing;
        private EditText qian;
        private Button JiaoFei;
//    支付
        private FuQianLaPay pay;

        private String jiaQian;

        private String name;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shuifeixiangqin_activity);

        httpQqiu();
        intiView();


    }

    private void httpQqiu() {
        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        name = bundle.getString("hao");
        OkGo.get(WanZhi.SHUIFEICHAXUN+name)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsoner = new JSONObject(s);
                            hao.setText(jsoner.getString("USERID"));
                            UserName.setText(jsoner.getString("USERNAME"));
                            yinJiao.setText(jsoner.getString("JE"));
                            qianfeijine.setText(jsoner.getString("JE"));
                            waiYe.setText(jsoner.getString("ZNJ"));
                            yuE.setText(jsoner.getString("AMT"));
                            JSONObject DATAMX = new JSONObject((Map) jsoner.getJSONObject("DATAMX"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });


    }

    private void intiView() {
//        返回与账单
        back = (LinearLayout) findViewById(R.id.ll_waterreat);
        xinaQi = (LinearLayout) findViewById(R.id.ll_xianqin);
        tvXiqnQing = (TextView) findViewById(R.id.tv_xiangqing);
//        应缴，用户编号，用户名称，欠费金额，违约金额，可用余额
        yinJiao = (TextView) findViewById(R.id.txt_userYJ);
        hao = (TextView) findViewById(R.id.tv_bianhao);
        UserName = (TextView) findViewById(R.id.tv_userName);
        qianfeijine = (TextView) findViewById(R.id.txt_userQF);
        waiYe = (TextView) findViewById(R.id.txt_userWY);
        yuE = (TextView) findViewById(R.id.txt_userYE);
//        输入缴费
        qian = (EditText) findViewById(R.id.waterrater_edt1);
//        按钮点击
        JiaoFei = (Button) findViewById(R.id.btn_waterbill);
        JiaoFei.setOnClickListener(this);
        back.setOnClickListener(this);
        tvXiqnQing.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_waterbill:

                jiaQian = qian.getText().toString();
                if (jiaQian.length() <= 0 || jiaQian == null) {
                    Toast.makeText(ShuiFeiXQ.this, "请输入缴费金额", Toast.LENGTH_SHORT).show();
                } else {

                pay = new FuQianLaPay.Builder(this)
                        .model(FuQianLa.MODEL_PRE_CHANNEL)//通道前置模式
                        .orderID(OrderUtils.getOutTradeNo())//订单号
                        .amount(Double.parseDouble(jiaQian))//金额
                        .subject("水费缴纳")//商品名称
                        .body("通道前置Activity-交易详情")//商品交易详情
                        .notifyUrl(Merchant.MERCHANT_NOTIFY_URL)
                        .build();
                pay.startPay(FuQianLa.ALI);
                }
                break;
            case R.id.ll_waterreat:
                finish();
                break;
            case R.id.tv_xiangqing:
                Intent intent = new Intent(ShuiFeiXQ.this,SfxqActivity.class);
                //用Bundle携带数据
                Bundle bundle=new Bundle();
                //传递name参数为tinyphp
                bundle.putString("yonhuhao",name);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }
}
